<?php 
	/**
	 * 自动注册爱情公寓id
	 */
	
	//判断爱情公寓抓下来的页面是否包含正常的uid（通过经验判断，审核不通过的uid页面会非常小）
	function checkIpartIdExists($page)
	{
		if(strlen($page)<200)
			return false;
		else
			return true;
	}
	//取得数据库中的最大uid
	function getIpartMaxId($ymd){
		global $conn;
		$tableName = getTableName($ymd);
		$sql = "select max(source_user_id) from $tableName where `from`='ipart' and fetch_date='$ymd'";
		$maxId = $conn->result($sql);
		if($maxId != false && $maxId > 0){
			return $maxId;
		}else{
			$ymd =  date("Y-m-d", time()-60*60*24);
			$tableName = getTableName($ymd);
			$sql = "select max(source_user_id) from $tableName where `from`='ipart' and fetch_date='$ymd'";
			$maxId = $conn->result($sql);
			return $maxId;
		}
	}
	//根据日期取得相应的表名
	function getTableName($dateString){
		$date_partial = explode('-', $dateString);
		$year = $date_partial[0];
		$month =$date_partial[1];
		if(intval($year)==2011 && intval($month)<=12){
			return "competitor";
		}else{
			return "competitor_{$year}{$month}";
		}
	}
	//奇虎360软件投票
	function qihu_poll()
	{
		$url = '';
		$opt_arr = array(CURLOPT_REFERER => "http://search.baidu.com/search",  //referer	
					);
		makeRequest($url, 
					"GET", 
					"", 
					true, 
					ROOT_PATH."/qihu/cookie/qihu_portal_cookie.txt",
					"",
					""
					);
	} 
	/*
	*一下抓取多个页面
	*/
	function makeIpartMultiRequests($uid, $number)
	{
		$mhandle = curl_multi_init();
		$handles = array();
		while($number>0){
		//http://www.ipart.cn/file/file_viewfile.php?u=11423089
			$url = "http://www.ipart.cn/file/file_viewfile.php?u=".$uid;
			$opt_arr = array(CURLOPT_REFERER => "http://search.baidu.com/search",  //referer	
					);
			$handles[$uid] = makeRequestResource($url,
												"GET" , 
												"", 
												false, 
												"", 
												ROOT_PATH."/ipart/cookie/ipart_login_cookie.txt", 
												$opt_arr
												);
			$uid++;
			$number--;
		}
		foreach($handles as $handle)
			curl_multi_add_handle($mhandle, $handle);
		//wait all request finish
		do {
			$status = curl_multi_exec($mhandle, $active);
		} while ($status === CURLM_CALL_MULTI_PERFORM || $active);
		curl_multi_close($mhandle);
		
		return $handles;
	}
	//生成爱情公寓登录后的cookie
	function makeIpartCookie()
	{
		//step1: browser the mainpage and get jsession id ,dump it to Ipart_portal_cookie.txt	
		makeRequest("http://www.ipart.com", 
					"GET", 
					"", 
					true, 
					ROOT_PATH."/ipart/cookie/ipart_portal_cookie.txt",
					"",
					""
					);
		//step2 login with the cookies which is fetched by previous request
		$parameter = array("_"=>time(), 
							"rememberMe"=>"true", 
							"type"=>'page', 
							"forward"=>'/personal/myindex.do',
							"user"=>"smarteng@qq.com", 
							"password"=>"789852"
							);
		makeRequest("http://www.ipart.com/login/user-login.do",
					"GET", 
					$parameter, 
					true, 
					ROOT_PATH."/ipart/cookie/ipart_login_cookie.txt", 
					ROOT_PATH."/ipart/cookie/ipart_portal_cookie.txt",
					""
					);
	}
		
	//对一个抓下的爱情公寓页面的后续处理
	function doWithIpartPage($uid, $ymd, $page)
	{
		//check uid exists?
		global $conn, $global_province_list;
		$tblName = getTblName();
		$sql = "select * from $tblName where `from`='ipart' and source_user_id='$uid' limit 1";
		$exists = $conn->has_record($sql);
		if ($exists){
			printlog("$uid exists, do nothing", "crawl");
			return 1;
		}
		else{
			$page = str_replace(array(" ","\t","\n","\r"), "", $page);
			// printlog("-----$page---------", "crawl");
			// exit;
			preg_match('/年龄：<\/span>(\d\d)/i', $page, $ages);
			$age = $ages[1];
			preg_match('/居住：<\/span>(.*?)->(.*?)<\/li>/i', $page, $area);
			$province = $area[1];
			$city = $area[2];
			
			//echo "age: $age province: $province city: $city tmp: $tmp ";exit;
			preg_match('/性别：<\/span>(.*?)生<\/li>/i',$page, $sex);
			$gender = $sex[1];
			$gender = ($gender=="男")?1:0; //男为1 女为0
			
			preg_match('/id="div_user_photo".*?<imgsrc="(.*?)"/i', $page, $matches);
			
			if($matches[1] ==''){
				$has_photo = 0 ;
			}else{
				$has_photo = 1 ;
			}
			
			//insert
			$arr = array("fetch_date" => $ymd,
					"`from`" => "ipart",
					"source_user_id" => $uid,
					"age" => $age,
					"province" => $province,
					"city" => $city,
					"gender" => $gender,
					"has_photo" => $has_photo,
				);
			$sql = Sql::insert($arr, $tblName);
			$conn->query($sql);
		}
	}
	//对一个抓下的爱情公寓页面的后续处理
	function doWithIpart($page)
	{
		//check uid exists?
		global $conn, $global_province_list;
			//extract
			//tmp: 山东济南34岁 或 江苏省20岁
			// echo $page;
			// exit;
			$tmp = simpleExtract('%profile_intro_row"><strong>(.*?)</strong>%i', $page, "tmp");
			$tmp = strip_tags($tmp);
			echo $tmp."<br />";
			
			$province = "";
			foreach ($global_province_list as $prov){
				if (strpos($tmp, $prov) !== false) {
					$province = $prov; //山东
					break;
				}
			}
			$age = simpleExtract('/(\d\d)/', $tmp, "age"); //20
			$city = simpleExtract('/来自(.*)?/', $tmp, "city"); //20
			$city = str_replace(array($province, $age, "省"), "", $city); //济南
			
			//echo "age: $age province: $province city: $city tmp: $tmp ";exit;
			$gender = simpleExtract('/(.*)?士/', $tmp, "gender");
			$gender = ($gender=="男")?1:0; //男为1 女为0
			
			preg_match('/<li.*?class="first-child".*?><img.*?src="(.*?)"/i', $page, $matches);
						
			$avat_array = array('300nv.png','300nj.png');
			if(isset($matches[1])){
				$photoarr = explode( '/' ,$matches[1]);
				if(is_array($photoarr)){
					$potofile = $photoarr[count($photoarr)-1];
					$has_photo = in_array($potofile,$avat_array)?0:1;
				}else{
					$has_photo = 0 ;
				}
			}else{
				preg_match('/<li.*?><img.*?name="profile_img".*?src="(.*?)"/i', $page, $match);
				if(isset($match[1])){
					$photoarray = explode( '/' ,$match[1]);
					if(is_array($photoarray)){
						$imgfile = $photoarray[count($photoarray)-1];
						$has_photo = in_array($imgfile ,$avat_array)?0:1;
					}else{
						$has_photo = 0 ;
					}
				}
			}
			echo "has_photo:".$has_photo."\n";
		
	}

?>